package com.sugon.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sugon.util.RestTemplateConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

@Service
public class VmsaService {

    public String getVideoToken(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("duration",1000);
        jsonObject.put("cids",538470264);

        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = null;
        try {
            restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        headers.set("cache-control", "no-cache");
        headers.set("content-type", "application/json");
        headers.set("x-app-id", "wbhl");
        headers.set("x-app-key", "800b0ff6e4c7b66137b22a8b87ead89e");
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
        String url = "http://172.11.16.248:10031/v2/devices/tokens";
//        String url = "https://jxsr-api.antelopecloud.cn/v2/devices/tokens";
        ResponseEntity<String> results = restTemplate.exchange(url, HttpMethod.POST, formEntity, String.class);
        JSONObject json = JSONObject.parseObject(results.getBody());
        String token = json.getJSONArray("tokens").getJSONObject(0).getString("token");
        return token;
    }

}
